package com.jx.mlxg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jx.mlxg.entity.Addresses;
import com.jx.mlxg.entity.Users;
import com.jx.mlxg.service.AddressesService;
import com.jx.mlxg.mapper.AddressesMapper;
import com.jx.mlxg.service.UsersService;
import com.jx.mlxg.utils.ResponseResult;
import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* @author Administrator
* @description 针对表【addresses】的数据库操作Service实现
* @createDate 2025-04-27 18:11:42
*/
@Service
public class AddressesServiceImpl extends ServiceImpl<AddressesMapper, Addresses>
    implements AddressesService{
    @Resource
    private UsersService usersService;

    @Override
    public ResponseResult<?> addAddress(Addresses address) {
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String openIdStr = (String) principal;
            // 根据 userId 获取用户信息和订单统计
            Users userInfoAndOrderStatistics = usersService.findByOpenId(openIdStr);
            address.setUserId(userInfoAndOrderStatistics.getUserId());
//            address.setPhoneNumber(userInfoAndOrderStatistics.getPhoneNumber());
            boolean saved = save(address);
            if (saved) {
                return ResponseResult.success(address);
            } else {
                return ResponseResult.failure("地址添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.failure("地址添加异常: " + e.getMessage());
        }
    }

    @Override
    public ResponseResult<?> updateAddress(Addresses address) {
        try {
            boolean updated = updateById(address);
            if (updated) {
                return ResponseResult.success(address);
            } else {
                return ResponseResult.failure("地址更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.failure("地址更新异常: " + e.getMessage());
        }
    }

    @Override
    public ResponseResult<?> deleteAddress(Integer addressId) {
        try {
            boolean deleted = removeById(addressId);
            if (deleted) {
                return ResponseResult.success(true);
            } else {
                return ResponseResult.failure("地址删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.failure("地址删除异常: " + e.getMessage());
        }
    }

    @Override
    public ResponseResult<?> getAddressById(Integer addressId) {
        try {
            Addresses address = getById(addressId);
            if (address != null) {
                return ResponseResult.success(address);
            } else {
                return ResponseResult.failure("地址不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.failure("获取地址异常: " + e.getMessage());
        }
    }

    @Override
    public ResponseResult<?> getAllAddresses() {
        try {
            // 从 SecurityContextHolder 中获取当前登录用户的信息
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String openIdStr = (String) principal;
            // 根据 userId 获取用户信息和订单统计
            Users userInfoAndOrderStatistics = usersService.findByOpenId(openIdStr);
            QueryWrapper<Addresses> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", userInfoAndOrderStatistics.getUserId()); // 添加条件：根据user_id查询
            // 按创建时间降序排序，新创建的地址显示在前面
            queryWrapper.orderByDesc("created_at");
            return ResponseResult.success(list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.failure("获取所有地址异常: " + e.getMessage());
        }
    }

}




